function getSchoolGrades() {
	console.log("----> Fetching grades");
	jQuery.ajax({
		url : url + "getSchoolGrades",
		type : "post",
		success : function(result) {
			console.log("Grade Result:",result);
			for (var i = 0; i < result.length; i++) {
				var obj = new Object();
				obj.ceGradeId = result[i].schoolGradeId;
				obj.gradeTitle = result[i].gradeName;
				obj.orderNo = result[i].orderNo;

				grades.push(obj);
			}

			if (aypId != "") {
				ko.dataFor(document.getElementById('create-Section')).OriginalgradesList(grades);
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("error getSchoolGrades");
		}
	});
}

function getSchoolAcademicSections(_type) {
	console.log("----> Fetching sections", _type, aypId);
	var type = _type;

	jQuery.ajax({
		data : {
			aypId : aypId,
			type : _type
		},
		url : url + "getAcademicSections",
		type : "post",
		success : function(result) {

			if (type == "simple") {
				sectionCategories = new Array();
				var obj = new Object();
				obj.sectionId = "";
				obj.sectionTitle = "School";
				sectionCategories.push(obj);

				for (var i = 0; i < result.length; i++) {
					//var sectionModel = new sectionModel();
					obj = new Object();
					obj.sectionId = result[i].sectionId;
					obj.sectionTitle = result[i].sectionTitle;
					sectionCategories.push(obj);
				}

				ko.dataFor(document.getElementById("add-Event")).availableCategories(sectionCategories);
			} else {
				isSectionsLoaded = true;
				var viewModel = ko.dataFor(document.getElementById("tabs-2"));

				for (var j = 0; j < result.length; j++) {
					var jsonData = ko.mapping.toJSON(result[j]);
					viewModel.sections.push(new sectionModel(jsonData, ""));

					for (var k = 0; k < result[j].grades.length; k++) {
						filterGradeList.push(result[j].grades[k].ceGradeId)
						console.log("filter data->", result[j].grades[k].ceGradeId);
					}
					console.log("filter length:", filterGradeList.length);
				}
				ko.dataFor(document.getElementById('create-Section')).OriginalgradesList(grades);

				viewModel.sections.sort(function(item1, item2) {
					console.log(item1, item2);
					if (item1.sectionTitle() > item2.sectionTitle()) {
						return 1;
					} else if (item1.sectionTitle() < item2.sectionTitle()) {
						return -1;
					} else {
						return 0;
					}

				});
			}

			// Will UnLoad Loader
			showLoader(false);

			/*
			if (type == "simple") {
			sectionCategories = new Array();

			var obj = new Object();
			obj.sectionId = "";
			obj.sectionTitle = "School";

			sectionCategories.push(obj);

			for (var i = 0; i < result.length; i++) {
			obj = new Object();
			obj.sectionId = result[i].sectionId;
			obj.sectionTitle = result[i].sectionTitle;

			sectionCategories.push(obj);
			}
			console.log("------>", sectionCategories.length);
			ko.dataFor(document.getElementById("add-Event")).availableCategories(sectionCategories);
			} else {

			var viewModel = ko.dataFor(document.getElementById("tabs-2"))

			sectionCategories = new Array();
			var obj = new Object();
			obj.sectionId = "";
			obj.sectionTitle = "School";
			sectionCategories.push(obj);

			console.log("viewModel--------->", viewModel);
			for (var i = 0; i < result.length; i++) {
			//var sectionModel = new sectionModel();

			obj = new Object();
			obj.sectionId = result[i].sectionId;
			obj.sectionTitle = result[i].sectionTitle;
			sectionCategories.push(obj);

			var jsonData = ko.mapping.toJSON(result[i]);
			var selectedItem = ko.mapping.fromJSON(jsonData);
			//console.log("data------>",jsonData);
			viewModel.sections.push(new sectionModel(jsonData, selectedItem));
			}

			ko.dataFor(document.getElementById("add-Event")).availableCategories(sectionCategories);
			}
			*/

			//   	ko.applyBindings(new eventsModel(),document.getElementById('tabs-3'));
			//	ko.applyBindings(new addEventViewModel(),document.getElementById('add-Event'));
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("error getSchoolAcademicSections");
		}
	});

}

function getAcademicYearPlan() {
	console.log("-------> get academic year plan",url );
	jQuery.ajax({
		data : {
			aypId : aypId
		},
		url : url + "getSchoolAcademic",
		type : "post",
		success : function(result) {
			console.log("getAcademicYearPlan result-->", result);

			var viewModel = ko.dataFor(document.getElementById("tabs-1"))
			console.log("viewModel->", viewModel);

			var strArr1 = result.startDate.split("-");
			var strArr2 = result.endDate.split("-");

			viewModel.startDate(strArr1[2] + "/" + strArr1[1] + "/" + strArr1[0]);
			viewModel.endDate(strArr2[2] + "/" + strArr2[1] + "/" + strArr2[0]);
			viewModel.title(result.title);
			viewModel.planStatus(result.planStatus);
			
			if(result.planStatus ==1)
			{
				editEventHolidayOnly(true);		
			}
			
			
			
			console.log("editEventHolidayOnly:",editEventHolidayOnly(),result.planStatus);
			for (var i = 0; i < result.holidays.length; i++) {
				var holidayView = new holiday();
				var obj = result.holidays[i];
				console.log("holiday view", holidayView);

				holidayView.title(obj.title);
				holidayView.startDate(obj.startDate);
				holidayView.endDate(obj.endDate);
				holidayView.eventId(obj.eventId);
				holidayView.eventSectionId("");
				//holidayView.eventType(0);
				holidayView.isUpdatable(0);
				//holidayView.duration(1);

				viewModel.holidays.push(holidayView);
			}

			console.log("---------> Sorting");
			
			jQuery(".ui-tooltip-content").hide();
	/*		viewModel.holidays.sort(function(item1, item2) {
				var item1Arr = item1.startDate().split("/");
				var item2Arr = item2.startDate().split("/");

				var firstDate = new Date(item1Arr[2], item1Arr[1]-1, item1Arr[0]);
				var lastDate = new Date(item2Arr[2], item2Arr[1]-1, item2Arr[0]);

				if (firstDate > lastDate) {
					console.log("->");
					return 1;
				} else if (firstDate < lastDate) {
					console.log("-->");
					return -1;
				} else {
					console.log("--->");
					return 0;
				}

			});*/
			
			console.log("viewModel.holidays",viewModel.holidays());
			isShowToolTip = true; // Will start showing tool tip in Date controller
			getSchoolAcademicSections("simple");
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("error getAcademicYearPlan");
		}
	});
}

function __getSchoolAcademicSections() {
	console.log("---> get school sections");
	jQuery.ajax({
		data : {
			aypId : aypId,
			type : ""
		},
		url : url + "getAcademicSections",
		type : "post",
		success : function(result) {
			//console.log("getSchoolAcademicSections result-->",result);

			var viewModel = ko.dataFor(document.getElementById("tabs-2"))
			console.log("viewModel--------->", viewModel);
			for (var i = 0; i < result.length; i++) {
				//var sectionModel = new sectionModel();

				var jsonData = ko.mapping.toJSON(result[i]);
				var selectedItem = ko.mapping.fromJSON(jsonData);
				//console.log("data------>",jsonData);
				viewModel.sections.push(new sectionModel(jsonData, selectedItem));
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("error __getSchoolAcademicSections");
		}
	});

}

var isEventsDataLoaded = false
var byCode = false
function getSchoolAcademicEvents(type) {
	console.log("-------> get academic events");
	jQuery.ajax({
		data : {
			aypId : aypId,
			eventType : "event"
		},
		url : url + "getSchoolAcademicEvents",
		type : "post",
		success : function(result) {
			if (type) {

				myAcademicYearModel.setEventData(result)
				showLoader(false);
				return result;
			}
			console.log("getSchoolAcademicEvents result-->", result);
			isEventsLoaded = true;
			var viewModel = ko.dataFor(document.getElementById("tabs-3"))
			console.log("viewModel:", viewModel);
			for (var i = 0; i < result.length; i++) {

				var obj = result[i];
				var category = ko.utils.arrayFirst(ko.dataFor(document.getElementById("add-Event")).availableCategories(), function(item) {
					return obj.eventSectionId === item.sectionId;
				});
				console.log("-------->", obj.eventSectionId);
				var durationVal = obj.duration == 0 ? "Half Day" : "All Day";
				console.log("-------->", aypId, obj.eventId, obj.title, obj.startDate, obj.endDate, category, obj.desc, durationVal, obj.eventSectionId);
				var eventModel = new eventVM(aypId, obj.eventId, obj.title, obj.startDate, obj.endDate, category, obj.desc, durationVal, obj.eventSectionId);
				console.log("-------->");
				viewModel.events.push(eventModel);

				console.log("---------> Sorting Events");
				viewModel.events.sort(function(item1, item2) {
					console.log(item1.startDate(), item2.startDate());
					var firstDate;
					var lastDate;
					var item1Arr = item1.startDate().split("/");
					var item2Arr = item2.startDate().split("/");

					firstDate = new Date(item1Arr[2], item1Arr[1], item1Arr[0]);
					lastDate = new Date(item2Arr[2], item2Arr[1], item2Arr[0]);

					if (firstDate > lastDate) {
						return 1;
					} else if (firstDate < lastDate) {
						return -1;
					} else {
						return 0;
					}

				});
			}

			showLoader(false);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("error getSchoolAcademicEvents");
		}
	});
}

function getWorkingDays() {
	console.log("fetching working days---->");
	jQuery.ajax({
		data : {
			aypId : aypId
		},
		url : url + "getAypWorkingDays",
		type : "post",
		success : function(result) {
			console.log("result-->", result);
			jQuery("#lblWorkingDays").text(result + " days");
			/*	if(typeof(result) == Number)
			 {
			 jQuery("#lblWorkingDays").text(result + " days");
			 }
			 else
			 {
			 jQuery("#lblWorkingDays").text('error');
			 }
			 */
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("error getWorkingDays");
		}
	});
}

